package com.sosee.cgfw.friends;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageButton;

import com.sosee.cgfw.R;
import com.sosee.cgfw.activityname.CaptureActivity;
import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;


/**
 * Created by Thinkpad on 2016/3/1.
 */
public class FashionFriends extends FragmentActivity implements View.OnClickListener {
    private ImageButton friends_back;
    private ImageButton friends_siaomiao;
    private ImageButton friends_friendslists;
    private ConversationListActivity conversationList;
    private static final int PHOTO_PIC = 1;
    private  String name;

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fashionfriends);
        enterFragment();
        init();
        provider();

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    //初始化控件
    private void init() {

        friends_back = (ImageButton) findViewById(R.id.friends_back);
        friends_siaomiao = (ImageButton) findViewById(R.id.friends_siaomiao);
        friends_friendslists = (ImageButton) findViewById(R.id.friends_friendslists);

        //3个按钮设置点击事件
        friends_back.setOnClickListener(this);
        friends_siaomiao.setOnClickListener(this);
        friends_friendslists.setOnClickListener(this);

    }

    private void enterFragment() {

        ConversationListFragment fragment = (ConversationListFragment) getSupportFragmentManager().findFragmentById(R.id.conversationlist);

        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话非聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//设置群组会话聚合显示
                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//设置讨论组会话非聚合显示
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//设置系统会话非聚合显示
                .build();

        fragment.setUri(uri);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.friends_back:
                finish();
                break;
            case R.id.friends_siaomiao:
                //打开扫描界面

                Intent intent3 = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent3, PHOTO_PIC);
//                startActivity(new Intent(this, FriendsScan.class));
                break;
            //打开好友列表
            case R.id.friends_friendslists:
                startActivity(new Intent(this, FriendsLists.class));
                break;
            default :

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        imgPath = null;
        if(resultCode == RESULT_OK){
            switch (requestCode) {
//                case CHOOSE_PIC:
//                    break;
                case PHOTO_PIC:
                    String result = data.getExtras().getString("result");
//                    Toast.makeText(FashionFriends.this, "结果:" + result, Toast.LENGTH_LONG).show();

                    Intent intent = new Intent(FashionFriends.this,FriendsScanResult.class);
                    intent.putExtra("togethers", result);

                    startActivity(intent);

                    break;

                default:
                    break;
            }
        }

    }

    private void provider() {

        RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
            @Override
            public UserInfo getUserInfo(String s) {
                return findUserById(s);
            }
        }, true);
    }

    private UserInfo findUserById(String s){

       String dd = SharedPreferencesUtils.getString(FashionFriends.this, "dd");
        if(null!=dd){

            Gson gson = new Gson();
            FriendsListData friendsListData = gson.fromJson(dd, FriendsListData.class);
            List<FriendsListData.Data> data = friendsListData.getData();

            for (int i=0;i<data.size(); i++) {
                //判断返回的userId
                if (data.get(i).getFriends().getID().equals(s)) {
                    if(null==data.get(i).getRemarks()||"".equals(data.get(i).getRemarks())||"-".equals(data.get(i).getRemarks())){
                       name = data.get(i).getFriends().getNickname();
                    }else{
                        name = data.get(i).getRemarks();
//                        data.get(i).getFriends().getNickname()
                    }
                    return new UserInfo(data.get(i).getFriends().getID(),name,Uri.parse(data.get(i).getFriends().getID()));
                }
            }
        }

        return null;
    }


}
